package com.nCoV.EpidemicSurveillance.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.nCoV.EpidemicSurveillance.common.utils.GetUserName;
import com.nCoV.EpidemicSurveillance.common.utils.UUIDUtils;
import com.nCoV.EpidemicSurveillance.domain.Regist;
import com.nCoV.EpidemicSurveillance.domain.SysPermission;
import com.nCoV.EpidemicSurveillance.domain.SysUser;
import com.nCoV.EpidemicSurveillance.mapper.SysPermissionMapper;
import com.nCoV.EpidemicSurveillance.mapper.SysUserMapper;


/** 
* @author 作者 罗穆健: 
* @version 创建时间：2020年1月5日 下午3:08:18 
* 类说明 
*/
@Service
public class UserServiceImpl  implements UserDetailsService{
	
	@Autowired
	SysUserMapper userMapper;

	@Autowired
	SysPermissionMapper sysPermissionMapper;


	@Override
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
		SysUser user = userMapper.findByUserName(username);
        if (user != null) {
        	List<SysPermission> permissions = sysPermissionMapper.findByAdminUserId(user.getId());
            List<GrantedAuthority> grantedAuthorities = new ArrayList <>();
            for (SysPermission permission : permissions) {
                if (permission != null && permission.getName()!=null) {

                    GrantedAuthority grantedAuthority = new SimpleGrantedAuthority(permission.getName());
                    //1：此处将权限信息添加到 GrantedAuthority 对象中，在后面进行权限验证时会使用GrantedAuthority 对象。
                    grantedAuthorities.add(grantedAuthority);
                }
            }
            return new User(user.getUsername(), user.getPassword(), grantedAuthorities);
        } else {
            throw new UsernameNotFoundException("用户名不存在");
        }
	}

	public String resetPassword(String newPassword) {
		try {
			userMapper.resetPassword(newPassword, GetUserName.getUserName());
			return "修改成功";
		} catch (Exception e) {
			return "false";
		}
		
	}
	
	@Transactional
	public Map<String, Object> registUser(Regist regist, String quanxian) {
		Map<String, Object> codeMap = new HashMap<String, Object>();
		try {
			if("1".equals(quanxian)) {
				regist.setRole("ROLE_JKRY");
			}else if("2".equals(quanxian)) {
				regist.setRole("ROLE_TEACHER");
			}
			String id = UUIDUtils.getUUID();
			regist.setId(id);
			userMapper.InsertUser(regist);
			userMapper.InsertUserRole(id,regist.getRole());
			codeMap.put("code", "1");
		} catch (Exception e) {
			System.out.println(e);
			// TODO: handle exception
			codeMap.put("code", "2");
		}
		return codeMap;
	}
	
}
 